<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class wms_ctl_admin_pickinglist extends desktop_controller{

    var $workground = 'wms_ctl_admin_pickinglist';
    var $pagelimit = 10;
    var $stocknotice_model ;
    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('pickinglist');
        header("cache-control: no-store, no-cache, must-revalidate");
    }
    //备货通知单首页
   function index()
   {

        $filter=array();

        $params = array(
            'title' => '备货审批表',
            'actions' => array(
                'new' => array(
                    'label' => '新建',
                    'href' => 'index.php?app=wms&ctl=admin_pickinglist&act=add_pickinglist',
                        'target' => '_blank',
                ),
                'confirm' => array(
                    'label' => '审批',
                    'submit' => 'index.php?app=wms&ctl=admin_pickinglist&act=doConfirm',    
                ),
                'undoconfirm' => array(
                    'label' => '取消审批',
                    'submit' => 'index.php?app=wms&ctl=admin_pickinglist&act=doundoConfirm',  
                ),
                'delete' => array(
                    'label' => '删除',
                    'submit' => 'index.php?app=wms&ctl=admin_pickinglist&act=doDelete',
                ),
            ),
            'base_filter' => $filter,
            //'base_filter' => '',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => true ,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'allow_detail_popup' => true,
            'max_actions' => 8,
            'use_view_tab' => false,

            'orderBy' => 'create_time desc',
        );
        $this->finder('wms_mdl_pickinglist', $params);

    }
    //添加备货
    function add_pickinglist()
    {
        $shopIds=implode("','", $this->shopIds);
        $sqlcustomer="SELECT distinct a.AXCustomer,a.customername  from sdb_wms_stockapp a 
                    inner join sdb_wms_stockapp_delis b on a.id=b.stockapp_id
                    inner join sdb_wms_delivery e on b.delivery_id=e.delivery_id
                    inner join sdb_ome_shop f on e.shop_id=f.shop_id
                where a.`status`='confirmed' and b.`status`='N' and f.shop_id in('$shopIds') and f.AXCustomer!='' and a.AXCustomer!=''  order by f.AXCustomer";
        $customer_data = kernel::database()->select($sqlcustomer);
        foreach ($customer_data as $key => $value) 
        {
            $customers[]= array('AXCustomer' => $value['AXCustomer'],'customername'=>'【'.$value['AXCustomer'].'】'.$value['customername']);   
        }
        $this->pagedata['pickinglist']['customers'] = $customers;

        $operator = kernel::single('desktop_user')->get_name();
        $this->pagedata['pickinglist']['operator'] = $operator;
        $this->pagedata['pickinglist']['pickinglist_bn']=$this->autocreatebn();
        //$this->pagedata['cur_date'] = date('YmdHis',time()).rand(1000,9999);
        //$this->singlepage("admin/purchase/purchase_add.html");
        $this->singlepage('admin/pickinglist/pickinglist_add.html');
    }
    function autocreatebn()
    {
        $bn='';
        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $time_begin= mktime(0,0,0,$m,$d,$y);
        $time_end= mktime(23,59,59,$m,$d,$y);
        $delivery_model = $this->app->model('pickinglist');
        $filter['create_time|between'] = array($time_begin, $time_end);
        $delivery= $delivery_model->getList('pickinglist_bn',$filter,0,1,'create_time desc');
        if($delivery[0]['pickinglist_bn']=='')
        {
            $bn ='SP'.date('Ymd',time()).'001';
        }
        else
        {
            $bn=substr($delivery[0]['pickinglist_bn'] , 10 , 3);
            $num=intval($bn)+1;
            if($num>9 && $num<=99)
            {
                $bn ='SA'.date('Ymd',time()).'0'.$num;
            }elseif($num<=9)
            {
                $bn ='SA'.date('Ymd',time()).'00'.$num;
            }else
            {
                $bn ='SA'.date('Ymd',time()).$num;
            }
        }
        return $bn;
    }

    /**
     * 备货申请表弹窗数据获取方法
     *
     * @param Void
     * @return String
     */
    function findstockapp($supplier_id=null)
    {
        //add by ldward 20180927 备货审批权限配置，根据客户编码查询所有对应的店铺数据
        $base_filter['print_status'] = '0';
        $base_filter['status'] = '0';
        $base_filter['is_ship'] = 'false';
        $base_filter['deliverytype'] = array('pickup','delivery','auto','batch','single');
        $base_filter['is_form'] = 'true';
        if(!$_POST['shop_id'])
        {
            $condition = $this->shopIds;
        }else{
            $condition = $_POST['shop_id'];
        }
        $shopIds=implode("','", $condition);

        $sqlcustomer="SELECT shop_id from sdb_ome_shop where shop_id in('$shopIds') order by AXCustomer";
        $customer_data = kernel::database()->select($sqlcustomer);
        foreach ($customer_data as $key => $value) 
        {
            $cdshopIds[]=$value['shop_id'];
        } 
        //判断下是否反选，如果都为空则正选，否则反着来

        $sqldelivery="SELECT b.delivery_id from sdb_wms_stockapp a inner join sdb_wms_stockapp_delis b on a.id=b.stockapp_id
                inner join sdb_wms_delivery_items items on b.delivery_id=items.delivery_id
                where a.`status`='confirmed' and b.`status`='N' ";
        if(empty($_GET['material']) && $_GET['AXCustomer']==0)
        {
            $sqldelivery.="";

        }elseif(!empty($_GET['material']) && $_GET['AXCustomer']==0)
        {
            $sqldelivery.=" and items.bn!='".$_GET['material']."'";

        }elseif(empty($_GET['material']) && $_GET['AXCustomer']!=0)
        {
            $sqldelivery.="and a.AXCustomer='".$_GET['AXCustomer']."'";
            
        }elseif(!empty($_GET['material']) && $_GET['AXCustomer']!=0)
        {
            $sqldelivery.="and a.AXCustomer='".$_GET['AXCustomer']."'";
            $sqldelivery.="and items.bn!='".$_GET['material']."'";
        }
        $delivery_data = kernel::database()->select($sqldelivery);
        foreach ($delivery_data as $key => $value) 
        {
            $deliveryIds[]=$value['delivery_id'];
        } 
        $base_filter['delivery_id']=$deliveryIds;
        $base_filter['shop_id']=$cdshopIds;


        //获取操作员管辖仓库
        if ($_GET['btype'] == 2) {
            //导出的时候走这里取第三方仓
            $ownerBranch = array();
            $ownerBranch = kernel::single('ome_branch_type')->getOtherBranchLists();
        } else {
            //所有自建仓
            $ownerBranch = array();
            $ownerBranch = kernel::single('ome_branch_type')->getOwnBranchIds();
        }

        # 获取操作员管辖仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super) 
        {
            $oBranch = app::get('ome')->model('branch');
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids) {
                $base_filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                $base_filter['ext_branch_id'] = array_intersect($base_filter['ext_branch_id'], $ownerBranch);
            } else {
                $base_filter['ext_branch_id'] = 'false';
            }
        } else 
        {
            $base_filter['ext_branch_id'] = $ownerBranch;
        }
        $params = array(
                'title'=>'发货单',
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
                'use_buildin_filter'=>true,
                'base_filter' => $base_filter,
        );
        $this->finder('wms_mdl_delivery', $params);
    }

    function getstockapps()
    {
        $omeMdl = app::get('ome')->model('orders');
        $omeExtOrdLib = kernel::single('ome_extint_order');
        $pro_id = $_POST['product_id'];
        if(!$_POST['shop_id']){
            $shopId = $this->shopIds;
        }else{
            $shopId = $_POST['shop_id'];
        }
        if (is_array($pro_id))
        {
            //多选判断
            if($pro_id[0]=='_ALL_')
            {
                
                $shopIds=implode("','", $shopId);

                $sqlcustomer="SELECT shop_id from sdb_ome_shop where shop_id in('$shopIds') order by AXCustomer";
                $customer_data = kernel::database()->select($sqlcustomer);
                foreach ($customer_data as $key => $value) 
                {
                    $cdshopIds[]=$value['shop_id'];
                } 
                //判断下是否反选，如果都为空则正选，否则反着来

                $sqldelivery="SELECT b.delivery_id from sdb_wms_stockapp a inner join sdb_wms_stockapp_delis b on a.id=b.stockapp_id
                        inner join sdb_wms_delivery_items items on b.delivery_id=items.delivery_id
                        where a.`status`='confirmed' and b.`status`='N' ";
                if(empty($_GET['material']) && $_GET['AXCustomer']==0)
                {
                    $sqldelivery.="";

                }elseif(!empty($_GET['material']) && $_GET['AXCustomer']==0)
                {
                    $sqldelivery.=" and items.bn!='".$_GET['material']."'";

                }elseif(empty($_GET['material']) && $_GET['AXCustomer']!=0)
                {
                    $sqldelivery.="and a.AXCustomer='".$_GET['AXCustomer']."'";
                    
                }elseif(!empty($_GET['material']) && $_GET['AXCustomer']!=0)
                {
                    $sqldelivery.="and a.AXCustomer='".$_GET['AXCustomer']."'";
                    $sqldelivery.="and items.bn!='".$_GET['material']."'";
                }
                $delivery_data = kernel::database()->select($sqldelivery);
                foreach ($delivery_data as $key => $value) 
                {
                    $deliveryIds[]=$value['delivery_id'];
                } 
                $filter['delivery_id']=$deliveryIds;
                $filter['shop_id']=$cdshopIds;
                
                //获取操作员管辖仓库
                if ($_GET['btype'] == 2) {
                    //导出的时候走这里取第三方仓
                    $ownerBranch = array();
                    $ownerBranch = kernel::single('ome_branch_type')->getOtherBranchLists();
                } else {
                    //所有自建仓
                    $ownerBranch = array();
                    $ownerBranch = kernel::single('ome_branch_type')->getOwnBranchIds();
                }

                # 获取操作员管辖仓库
                $is_super = kernel::single('desktop_user')->is_super();
                if (!$is_super) {
                    $oBranch = app::get('ome')->model('branch');
                    $branch_ids = $oBranch->getBranchByUser(true);
                    if ($branch_ids) {
                        $filter['ext_branch_id'] = $_POST['branch_id'] ? $_POST['branch_id'] : $branch_ids;

                        $filter['ext_branch_id'] = array_intersect($filter['ext_branch_id'], $ownerBranch);
                    } else {
                        $filter['ext_branch_id'] = 'false';
                    }
                } else {
                    $filter['ext_branch_id'] = $ownerBranch;
                }

            }else{
                $filter['delivery_id'] = $pro_id;
            }
        }
         if (isset($_POST['filter']['advance']) && $_POST['filter']['advance'])
        {
            $arr_filters    = explode(',', $_POST['filter']['advance']);
            foreach ($arr_filters as $obj_filter)
            {
                $arr    = explode('=', $obj_filter);
                $filter[$arr[0]]    = $arr[1];
            }
            if($filter['_delivery_time_search'] == 'than' || $filter['_delivery_time_search'] == 'lthan'){
                $filter['_DTYPE_TIME'][0] = $filter['_DTYPE_TIME[]'];
                $filter['_DTIME_']['H']['delivery_time'] = $filter['_DTIME_[H][delivery_time]'];
                $filter['_DTIME_']['M']['delivery_time'] = $filter['_DTIME_[M][delivery_time]'];
            }
            if($filter['_delivery_time_search'] == 'between'){
                $filter['_DTYPE_TIME'][0] = $filter['_DTYPE_TIME[]'];
                $filter['_DTIME_']['H']['delivery_time_from'] = $filter['_DTIME_[H][delivery_time_from]'];
                $filter['_DTIME_']['M']['delivery_time_from'] = $filter['_DTIME_[M][delivery_time_from]'];
                $filter['_DTIME_']['H']['delivery_time_to'] = $filter['_DTIME_[H][delivery_time_to]'];
                $filter['_DTIME_']['M']['delivery_time_to'] = $filter['_DTIME_[M][delivery_time_to]'];
            }
        }
        $filter['print_status'] = '0';
        $filter['status'] = '0';
        $filter['is_ship'] = 'false';
        $filter['is_form'] = 'true';
        $filter['deliverytype'] = array('pickup','delivery','auto','batch','single');

        $delivery_model = $this->app->model('delivery');
        $stockappDelisMdl = $this->app->model('stockapp_delis');
        $Oshop = app::get('ome')->model('shop');
        $delivery= $delivery_model->getList('delivery_bn,delivery_id,shop_id,outer_delivery_bn',$filter);
        $db=kernel::database();
        if (!empty($delivery))
        {
            foreach ($delivery as $k => $item)
            {
                $item['id']=$item['delivery_id'];
                $item['product_id']=$item['delivery_id'];
                $item['delivery_bn']=$item['delivery_bn'];

                $stock_data =  $db->select("SELECT a.shop_id,a.shop_name,b.AXCustomer,b.customername,b.stockapp_bn,a.stockapp_id,a.bnsContent FROM sdb_wms_stockapp_delis a
                    inner join sdb_wms_stockapp b on a.stockapp_id=b.id where a.delivery_id='".$item['delivery_id']."'");
                $item['shop_id']=$item['shop_id'];
                $item['shop_name']=$stock_data[0]['shop_name'] ;
                $item['stockapp_id']=$stock_data[0]['stockapp_id'] ;
                $item['stockapp_bn']=$stock_data[0]['stockapp_bn'] ;  
                $item['AXCustomer']=$stock_data[0]['AXCustomer'];     
                $item['customername']=$stock_data[0]['customername']; 
                $cnts = unserialize($stock_data[0]['bnsContent']);
                $item['bnsContent']=implode(',',$cnts);
                $rows[]= $item;

            }
        }
        echo "window.autocompleter_json=".json_encode($rows);
    }
    ///显示汇总type=0所有，type=1一单
    function showlist()
    {
        if($_GET['type']==0)
        {          
            $sql = "select os.order_bn,e.delivery_bn,db.logi_no ,pickLine.last_modify ,f.AXCustomer,f.customername 
                    from  sdb_wms_delivery e  
                    inner join sdb_wms_delivery_bill db on e.delivery_id = db.delivery_id 
                    inner JOIN sdb_ome_delivery od on ( e.outer_delivery_bn = od.delivery_bn ) 
                    inner JOIN sdb_ome_delivery_order odo on ( od.delivery_id = odo.delivery_id ) 
                    inner JOIN sdb_ome_orders os on (odo.order_id = os.order_id) 
                    inner join sdb_wms_pickinglist_line pickLine on pickLine.delivery_id=e.delivery_id
                    inner join sdb_ome_shop f on e.shop_id=f.shop_id
                    where pickLine.pickinglist_id= ".$_GET['id'];

        }elseif($_GET['type']==1)
        {
            $sql = "select os.order_bn,e.delivery_bn,db.logi_no ,dli.last_modify ,f.AXCustomer,f.customername 
                    from sdb_wms_pickinglist_line dli 
                    inner join  sdb_wms_delivery e on dli.delivery_id=e.delivery_id 
                    inner join sdb_wms_delivery_bill db on e.delivery_id = db.delivery_id 
                    inner JOIN sdb_ome_delivery od on ( e.outer_delivery_bn = od.delivery_bn ) 
                    inner JOIN sdb_ome_delivery_order odo on ( od.delivery_id = odo.delivery_id ) 
                    inner JOIN sdb_ome_orders os on (odo.order_id = os.order_id) 
                    inner join sdb_ome_shop f on e.shop_id=f.shop_id
                    where dli.stockapp_id= ".$_GET['id'];
        }
        $rows =  kernel::database()->select($sql);
        foreach ($rows as $key => $value) 
        {
            $rows[$key]['last_modify']=date('Y-m-d H:i',$value['last_modify']);
        }
        $this->pagedata['res'] = $rows;
        $this->singlepage('admin/pickinglist/pickinglist_showlist.html');
    }
    //保存备货通知单
    function doSave()
    {
        $this->begin();
        $pickinglist_model = $this->app->model('pickinglist');
        $stockapp_model = $this->app->model('stockapp');
        $orders_obj = $this->app->model('delivery');
        $pickinglistDeil_model = $this->app->model('pickinglist_line');
        $stockapp_ID = $_POST['stockapp_ID'];

        if(empty($stockapp_ID))
        {
            $this->end(false, '无备货申请明细，请确认！');
        }
        $db=kernel::database();
        if(empty($_POST['id']))
        {

            $name = $this->autocreatebn();
            $memo = $_POST['memo'];
            $operator = $_POST['operator'];

            $pickinglist_id = $pickinglist_model ->getList('id',array('pickinglist_bn'=>$_POST['purchase_name']));
            if ($pickinglist_id) 
            {
                $this->end(false, '存在多人同时操作，请稍后重试');
            }

            $arr_order='';
            $arr_order['pickinglist']['pickinglist_bn']=$this->autocreatebn();//$_POST['purchase_name'];
            $arr_order['pickinglist']['remark']=$_POST['memo'];
            $arr_order['pickinglist']['operator']=$_POST['operator'];
            $arr_order['pickinglist']['create_time']=time();  
            $res = $pickinglist_model->insert($arr_order['pickinglist']);
        }else{
            $res=$_POST['id'];
            $params=array(
                'id'=>$res,
                'remark'=>$_POST['memo'],
            );
            $pickinglist_model->save($params);
            $db->exec("update sdb_wms_stockapp_delis set status = 'N',last_modify='".time()."' where  delivery_id in(
                        select delivery_id from sdb_wms_pickinglist_line where pickinglist_id in (".$res."));") ;
            $db->exec("delete from sdb_wms_pickinglist_line  where  pickinglist_id in(" . $res . ");") ;
        }
        foreach ($stockapp_ID as $key => $value)
        {
            $stockapp_data =  $db->select("SELECT a.id,a.delivery_id,a.delivery_bn,a.shop_id,a.shop_name,b.AXCustomer,b.customername,b.stockapp_bn,a.stockapp_id,b.create_time,b.operator,a.bnsContent FROM sdb_wms_stockapp_delis a
                    inner join sdb_wms_stockapp b on a.stockapp_id=b.id where a.delivery_id='".$value."'");
            
            $arr_line='';
            $arr_line['pickinglist']['pickinglist_id']=$res;
            $arr_line['pickinglist']['delivery_bn']=$stockapp_data[0]['delivery_bn'];
            $arr_line['pickinglist']['delivery_id']=$stockapp_data[0]['delivery_id'];
            $arr_line['pickinglist']['stockapp_bn']=$stockapp_data[0]['stockapp_bn'];
            $arr_line['pickinglist']['stockapp_id']=$stockapp_data[0]['stockapp_id'];
            $arr_line['pickinglist']['shop_id']=$stockapp_data[0]['shop_id'];
            $arr_line['pickinglist']['shop_name']=$stockapp_data[0]['shop_name'];
            $arr_line['pickinglist']['AXCustomer']=$stockapp_data[0]['AXCustomer'];
            $arr_line['pickinglist']['customername']=$stockapp_data[0]['customername'];
            $arr_line['pickinglist']['app_time']=$stockapp_data[0]['create_time'];
            $arr_line['pickinglist']['operator']=$stockapp_data[0]['operator'];
            $arr_line['pickinglist']['bnsContent']=$stockapp_data[0]['bnsContent'];
            $pickinglistDeil_model->insert($arr_line['pickinglist']);
            $db->exec("update sdb_wms_stockapp_delis set status = 'Y',last_modify='".time()."' where  delivery_id in(".$value.");") ;
        }
        $this->end(true, '已完成');
    }
    function editpickinglist()
    {
        $shopIds=implode("','", $this->shopIds);
        $sqlcustomer="SELECT distinct AXCustomer,customername 
            from sdb_ome_shop f 
            where f.shop_id in('$shopIds') and f.AXCustomer!=''  order by f.AXCustomer";
        $customer_data = kernel::database()->select($sqlcustomer);
        foreach ($customer_data as $key => $value) 
        {
            $customers[]= array('AXCustomer' => $value['AXCustomer'],'customername'=>'【'.$value['AXCustomer'].'】'.$value['customername']);   
        }
        $this->pagedata['pickinglist']['customers'] = $customers;

        $pickinglist_model = $this->app->model('pickinglist');
        $pickinglist_datas = $pickinglist_model->getList('*', array('id'=>$_GET['id']), 0, -1);

        $this->pagedata['pickinglist'] = $pickinglist_datas[0];
        $this->singlepage('admin/pickinglist/pickinglist_edit.html');
    }

    function getEditProducts($po_id){
        if ($po_id == ''){
            $po_id = $_POST['p[0]'];
        }
        $piObj = $this->app->model('pickinglist_line');
        $pickinglist_line = $piObj->getList('*',array('pickinglist_id'=>$po_id),0,-1);
        foreach ($pickinglist_line as $key => $value) 
        {
            $row['shop_id']=$value['shop_id'];
            $row['shop_name']=$value['shop_name'];
            $row['delivery_id']=$value['delivery_id'];
            $row['delivery_bn']=$value['delivery_bn'];
            $row['product_id']=$value['delivery_id'];
            $row['stockapp_bn']=$value['stockapp_bn']; 
            $row['AXCustomer']=$value['AXCustomer'];     
            $row['customername']=$value['customername'];
            $cnts = unserialize($value['bnsContent']);
            $row['bnsContent']=implode(',',$cnts);
            $row['id']=$value['delivery_id'];
            $items[]=$row;
        }
        echo json_encode($items);
    }

    //审批动作
    function doConfirm()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_pickinglist&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();
        $operator = kernel::single('desktop_user')->get_name();
        $db=kernel::database();

        $picklist_model = $this->app->model('pickinglist');
        if ($order_info['isSelectedAll']=='_ALL_') 
        {
            foreach ($orders as $key_o => $order) 
            {  
                $picklist = $picklist_model->getList('*', array('id'=>$value), 0, 1);
                if($picklist[0]['status']=='unconfirmed')
                {
                    // add by ldward 20180928 备货审批按钮权限校验；

                    if($picklist[0]['operator']!=$operator)
                    {
                        $this->end(false, "审批单号".$picklist[0]['pickinglist_bn']."您无权限审批，请确认！");
                    }
                    $value = $order['id']; 
                    $db->exec("UPDATE sdb_wms_pickinglist set status = 'confirmed',last_modify='".time()."' where  id in(".$value.");");
                    $db->exec("UPDATE sdb_wms_delivery d, sdb_wms_pickinglist_line ppp
                        SET d.is_ship ='true',d.deliverynote_time='".time()."' WHERE d.delivery_id=ppp.delivery_id and ppp.pickinglist_id in('".$value."');");
                }
            }

        }else{
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value)
            {   
                $picklist = $picklist_model->getList('*', array('id'=>$value), 0, 1);
                if($picklist[0]['status']=='unconfirmed')
                {
                    if($picklist[0]['operator']!=$operator)
                    {
                        $this->end(false, "审批单号".$picklist[0]['pickinglist_bn']."您无权限审批，请确认！");
                    }
                    $db->exec("UPDATE sdb_wms_pickinglist set status = 'confirmed',last_modify='".time()."' where  id in(".$value.");");
                    $db->exec("UPDATE sdb_wms_delivery d, sdb_wms_pickinglist_line ppp
                        SET d.is_ship ='true',d.deliverynote_time='".time()."' WHERE d.delivery_id=ppp.delivery_id and ppp.pickinglist_id in('".$value."');");
                }
            }
        }      //
        $this->end(true, '已完成');
    }
    //取消审批动作
    function doundoConfirm()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_pickinglist&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();
        $operator = kernel::single('desktop_user')->get_name();
        $db=kernel::database();
        $picklist_model = $this->app->model('pickinglist');
        if ($order_info['isSelectedAll']=='_ALL_') 
        {
            foreach ($orders as $key_o => $order) 
            {
                $value = $order['id'];
                $picklist = $picklist_model->getList('*', array('id'=>$value), 0, 1);
                if($picklist[0]['status']=='confirmed')
                {
                    if($picklist[0]['operator']!=$operator)
                    {
                        $this->end(false, "审批单号".$picklist[0]['pickinglist_bn']."您无权限取消审批，请确认！");
                    }

                    $db->exec("UPDATE sdb_wms_pickinglist set status = 'unconfirmed',last_modify='".time()."' where  id in(".$value.")");
                 
                    $db->exec("UPDATE sdb_wms_delivery d, sdb_wms_pickinglist_line ppp
                    SET d.is_ship ='false',d.deliverynote_time='".time()."' WHERE d.delivery_id=ppp.delivery_id and ppp.pickinglist_id in('".$value."');");
                }
            }
        }else{
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value)
            {
                $picklist = $picklist_model->getList('*', array('id'=>$value), 0, 1);
                if($picklist[0]['status']=='confirmed')
                {
                    if($picklist[0]['operator']!=$operator)
                    {
                        $this->end(false, "审批单号".$picklist[0]['pickinglist_bn']."您无权限取消审批，请确认！");
                    }

                    $db->exec("UPDATE sdb_wms_pickinglist set status = 'unconfirmed',last_modify='".time()."' where  id in(".$value.")");
                 
                    $db->exec("UPDATE sdb_wms_delivery d, sdb_wms_pickinglist_line ppp
                    SET d.is_ship ='false',d.deliverynote_time='".time()."' WHERE d.delivery_id=ppp.delivery_id and ppp.pickinglist_id in('".$value."');");
                }else
                {
                    $this->end(false, '当前状态不为已完成，取消失败！');
                }
            }
        }
        $this->end(true, '已完成');
    }
    //删除动作
    function doDelete()
    {
        $max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_pickinglist&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $pickinglist_delis_model = $this->app->model('pickinglist_line');
        $pickinglist_model = $this->app->model('pickinglist');
        $db=kernel::database();
        if ($order_info['isSelectedAll']=='_ALL_') {
            $orders = $pickinglist_model->getList('*', array(), 0, -1);
            foreach ($orders as $key => $order) 
            {
                $value = $order['id'];
                if ($order['status'] == 'unconfirmed') 
                {
                    $db->exec("update sdb_wms_stockapp_delis set status = 'N',last_modify='".time()."' where  delivery_id in(
                        select delivery_id from sdb_wms_pickinglist_line where pickinglist_id in (".$value."));") ;
                    $db->exec("delete from sdb_wms_pickinglist_line  where  pickinglist_id in(" . $value . ");") ;
                    $pickinglist_model->db->exec("delete from sdb_wms_pickinglist  where  id in(" . $value . ");");
                } else 
                {
                    $this->end(false, '备货单号【' . $order['pickinglist_bn'] . '】状态已审批，删除失败！');
                }
            }
        } else {
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }
            foreach ($order_info['id'] as $key_o => $value) {
                $orders = $pickinglist_model->getList('*', array('id' => $value), 0, 1);

                if ($orders[0]['status'] == 'unconfirmed') 
                {
                    $db->exec("update sdb_wms_stockapp_delis set status = 'N',last_modify='".time()."' where  delivery_id in(
                        select delivery_id from sdb_wms_pickinglist_line where pickinglist_id in (".$value."));") ;
                    $db->exec("delete from sdb_wms_pickinglist_line  where  pickinglist_id in(" . $value . ");") ;
                    $pickinglist_model->db->exec("delete from sdb_wms_pickinglist  where  id in(" . $value . ");");
                } else 
                {
                    $this->end(false, '备货单号【' . $orders[0]['pickinglist_bn'] . '】状态已审批，删除失败！');
                }
            }
        }
        $this->end(true, '已完成');
    }
}
